package com.cymaybe.foucssurfaceview.fragment;

import android.app.DialogFragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cymaybe.foucssurfaceview.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 *
 */

public class PictureFragment extends DialogFragment {
    public static final String ORIGIN_PICTURE = "originPic";
    public static final String CROP_PICTURE = "cropPic";

    private Bitmap mOriginPicBitmap, mCropPicBitmap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle data = getArguments();
        if (data != null) {
            mOriginPicBitmap = data.getParcelable(ORIGIN_PICTURE);
            mCropPicBitmap = data.getParcelable(CROP_PICTURE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.picture_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
//        ImageView originImg = (ImageView) view.findViewById(R.id.origin_picture_img);
//        originImg.setImageBitmap(mOriginPicBitmap);
        ImageView cropImg = (ImageView) view.findViewById(R.id.crop_picture_img);
        cropImg.setImageBitmap(mCropPicBitmap);
        saveMyBitmap(mCropPicBitmap);
    }


    private String i;

    public void saveMyBitmap(Bitmap mBitmap) {
        i= UUID.randomUUID().toString().substring(5);
        File dir = new File("/sdcard/Note/");
        if (!dir.exists())
            dir.mkdirs();
        File f = new File("/sdcard/Note/" + i+ ".jpg");
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.e("FFF", e.getMessage());
        }
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("FFF", e.getMessage());
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("FFF", e.getMessage());
        }
    }

    /**
     * @param bitmap  需要保存的图片对象
     * @param picPath 需要保存的本地路径，建议设置为公用常量
     */
    private void saveBitmap(Bitmap bitmap, String picPath) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(picPath));
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * @param imageView 需要设置图片的控件
     * @param picPath   需要读取图片的本地路径
     * @return
     */
    private void readBitmap(ImageView imageView, String picPath) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(picPath));
            Bitmap bitmap = BitmapFactory.decodeStream(fis);
            imageView.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
